package org.zjkoye.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zjkoye.crm.entity.Business;
import org.zjkoye.crm.entity.Workorder;
import org.zjkoye.crm.mapper.BusinessMapper;
import org.zjkoye.crm.mapper.WorkorderMapper;
import org.zjkoye.crm.service.BusinessService;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zjkoye
 * @since 2024-06-23
 */
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;
//    @Override
//
//
//    @Override
//    public boolean updateUser(Business user)
//    {
//
//    }
//
    @Autowired
    private WorkorderMapper workorderService;
    @Override
    public void updateBusinessWorkOrderStatus() {
        // 获取所有工单的状态
        List<Workorder> workOrders = workorderService.selectList(null);
        for (Workorder workOrder : workOrders) {
            businessMapper.updatesame(workOrder.getBusinessId(),workOrder.getStatus());
            // 根据工单ID更新业务表中的状态
            //workorderService.updateStatusByWorkorderId(workOrder.getWorkOrderId(), workOrder.getStatus());
        }
    }
    @Override
    public boolean deleteById(int id)
    {
        return businessMapper.deleteById(id) > 0;
    }

    @Override
    public  Business getCustomerById(int id)
    {
        return businessMapper.selectById(id);
    }

    @Override
    public IPage<Business> pageList(int pageSize, int currentPage, String name)
    {

        // like 条件构造
        LambdaQueryWrapper<Business> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null && !"".equals(name), Business::getBusinessName, name);
        // 分页构造

        IPage<Business> page = new Page<>(currentPage, pageSize);
        businessMapper.selectPage(page, wrapper);
        return page;
    }

}
